package com.ewzj.bigdata.mvp.presenter;

import android.app.Activity;
import android.app.Application;

import com.alibaba.sdk.android.oss.ClientException;
import com.alibaba.sdk.android.oss.ServiceException;
import com.alibaba.sdk.android.oss.callback.OSSCompletedCallback;
import com.alibaba.sdk.android.oss.callback.OSSProgressCallback;
import com.alibaba.sdk.android.oss.model.OSSRequest;
import com.alibaba.sdk.android.oss.model.OSSResult;
import com.alibaba.sdk.android.oss.model.PutObjectRequest;
import com.ewzj.bigdata.app.ossupload.CustomSignSample;
import com.ewzj.bigdata.app.utils.Apputils;
import com.ewzj.bigdata.app.utils.RxUtils;
import com.ewzj.bigdata.mvp.contract.EditProfileContract;
import com.ewzj.bigdata.mvp.model.entity.resbean.BaseJson;
import com.ewzj.bigdata.mvp.model.entity.resbean.UserBean;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.http.imageloader.ImageLoader;
import com.jess.arms.integration.AppManager;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.utils.ArmsUtils;

import javax.inject.Inject;

import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;


@ActivityScope
public class EditProfilePresenter extends BasePresenter<EditProfileContract.Model, EditProfileContract.View> {
    private RxErrorHandler mErrorHandler;
    private Application mApplication;
    private ImageLoader mImageLoader;
    private AppManager mAppManager;
    private Activity activity;
    CustomSignSample customSignSample;
    @Inject
    public EditProfilePresenter(EditProfileContract.Model model, EditProfileContract.View rootView
            , RxErrorHandler handler, Application application
            , ImageLoader imageLoader, AppManager appManager) {
        super(model, rootView);
        this.mErrorHandler = handler;
        this.mApplication = application;
        this.mImageLoader = imageLoader;
        this.mAppManager = appManager;
        this.activity= (Activity) mRootView;
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
    public void uploadFile(String path){
        customSignSample=new CustomSignSample(path);
        customSignSample.customSignUpload(activity, path, new OSSProgressCallback() {
            @Override
            public void onProgress(Object request, long currentSize, long totalSize) {
                int progress= (int) (currentSize/totalSize);
//                mRootView.setUploadProgress(progress);
            }
        }, new OSSCompletedCallback() {
            @Override
            public void onSuccess(OSSRequest request, OSSResult result) {
                ArmsUtils.snackbarText("图片上传成功");
                PutObjectRequest putObjectRequest= (PutObjectRequest) request;
                mRootView.setUserImg(Apputils.getOssFileUrl(putObjectRequest.getObjectKey()));
            }

            @Override
            public void onFailure(OSSRequest request, ClientException clientException, ServiceException serviceException) {

            }
        });
    }

    public void updateUserInfo(UserBean userBean) {
        mModel.updateUserInfo(userBean)
                .compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<BaseJson>(mErrorHandler) {
                    @Override
                    public void onNext(BaseJson baseJson) {
                        if (baseJson!=null){
                            ArmsUtils.snackbarText("修改资料成功");
                        }
                    }
                });
    }

    public void cancelUpload(){
        if (customSignSample!=null){
            customSignSample.cancel();
        }
    }
}
